package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.CollectionBean;

import java.util.List;


public class CollectionAdapter extends RecyclerView.Adapter {

    private static final int MYLIVE_MODE_CHECK = 0;

    int mEditMode = MYLIVE_MODE_CHECK;

    Context context;

    private List<CollectionBean> mDates;

    private OnItemClickListener mOnItemClickListener;

    public CollectionAdapter(Context context, List<CollectionBean> date) {
        this.context = context;
        this.mDates = date;
    }

    public void notifyAdapter(List<CollectionBean> myLiveList, boolean isAdd) {
        if (!isAdd) {
            this.mDates = myLiveList;
        } else {
            this.mDates.addAll(myLiveList);
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClickListener(int pos, List<CollectionBean> myLiveList);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_collection, parent, false);
        return new CollectionViewHolder(view);
    }

    @Override
    public int getItemCount() {
        return mDates.size();
    }

    public void setEditMode(int editMode) {
        mEditMode = editMode;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final CollectionViewHolder viewHolder = (CollectionViewHolder) holder;
        final CollectionBean bean = mDates.get(holder.getAdapterPosition());

        viewHolder.tvTitle.setText(mDates.get(position).getTitle());
        viewHolder.tvContent.setText(mDates.get(position).getContent());
        viewHolder.tvPrice.setText("¥" + mDates.get(position).getPrice());
        Glide.with(context).load(mDates.get(position).getImgResources()).into(viewHolder.ivGoods);

        if (mEditMode == MYLIVE_MODE_CHECK) {
            viewHolder.checkBox.setVisibility(View.GONE);
        } else {
            viewHolder.checkBox.setVisibility(View.VISIBLE);

            if (bean.isSelect()) {
                viewHolder.checkBox.setImageResource(R.drawable.icon_choose);
            } else {
                viewHolder.checkBox.setImageResource(R.drawable.icon_unchoose);
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(holder.getAdapterPosition(), mDates);
            }
        });
    }

}
